package com.example.springboot.service.admin.Impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.springboot.mapper.MusicInfoMapper;
import com.example.springboot.model.entity.MusicInfoEntity;
import com.example.springboot.model.form.PageUtil;
import com.example.springboot.model.req.admin.MusicInfoReq;
import com.example.springboot.model.resp.MusicInfoResp;
import com.example.springboot.service.admin.MusicInfoService;
import com.example.springboot.utils.CopyUtil;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 音乐基本信息表 服务实现类
 * </p>
 *
 * @author zf
 * @since 2023-12-15
 */
@Service
public class MusicInfoServiceImpl extends ServiceImpl<MusicInfoMapper, MusicInfoEntity> implements MusicInfoService {

    @Override
    public Page<MusicInfoResp> getPage(MusicInfoReq req) {
        IPage<MusicInfoEntity> ipage = page(req.toPage(), new LambdaQueryWrapper<MusicInfoEntity>()
                .like(ObjectUtil.isNotNull(req.getStatus()) && req.getStatus() == 1 && StringUtils.isNotBlank(req.getMusicName()),MusicInfoEntity::getMusicName, req.getMusicName())
                .like(ObjectUtil.isNotNull(req.getStatus()) && req.getStatus() == 2 && StringUtils.isNotBlank(req.getMusicName()),MusicInfoEntity::getSinger, req.getMusicName())
                .eq(MusicInfoEntity::getDelFlag, 0));

        Page<MusicInfoEntity> page = PageUtil.toPage(ipage);
        return PageUtil.convert(page, e -> {
        MusicInfoResp resp = CopyUtil.copyObject(e, MusicInfoResp.class);
            return resp;
        });
    }
    @Override
    public Boolean addInfo(MusicInfoEntity req) {
        //主表复制元素
//      MusicInfoEntity entity = CopyUtil.copyObject(req, MusicInfoEntity.class);
        boolean flag;
        if (req.getId() != null) {
            flag = updateById(req);
        } else {
            flag = save(req);
        }
        return flag;
    }

    @Override
    public MusicInfoResp getInfo(Long id) {
        MusicInfoResp resp = CopyUtil.copyObject(getById(id), MusicInfoResp.class);
        resp.setLyricText(musicText(resp.getLyricUrl()));
        return resp;
    }

    public String musicText(String lyricUrl){
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(lyricUrl.replace(" ", "%20"));
        httpGet.addHeader("Content-Type", "application/json");
        String musicText = "";
        try {
            HttpResponse response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == 200) {
                musicText = EntityUtils.toString(response.getEntity(),"UTF-8");
            }else {
                log.error("歌词获取异常:"+response.getStatusLine().getStatusCode() );
            }
        }catch (Exception e){
            log.error("歌词获取异常："+e);
        }
        return musicText;
    }

    @Override
    public Boolean deleteInfos(String ids) {
        return update(new UpdateWrapper<MusicInfoEntity>().lambda()
                .set(MusicInfoEntity::getDelFlag, 1)
                .inSql(MusicInfoEntity::getId, ids)
        );
    }
}
